# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=librasterlite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=1.1.0-beta1
pkgver=1.1.0
pkgrel=3
pkgdesc='An open source library that stores and retrieves huge raster coverages using a SpatiaLite DBMS (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.gaia-gis.it/fossil/librasterlite2/index'
license=('GPLv3+')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libspatialite")
options=('strip' 'staticlibs')
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-sources/${_realname}-${_pkgver}.tar.gz"
        "001-fix-mingw-detection.patch"
        "002-fix-linking-on-clang.patch")
sha256sums=('f7284cdfc07ad343a314e4878df0300874b0145d9d331b063b096b482e7e44f4'
            'f4a9b63767f8ed0e73085a731ffdd9d4d0d8044a7a1976210c930c06c6bc4ecc'
            '088c975841a8cec6971db5b46194e1d8e627808d4a3034dc27f25a5bffd3ac15')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  patch -p1 -i ${srcdir}/001-fix-mingw-detection.patch
  patch -p1 -i ${srcdir}/002-fix-linking-on-clang.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}/build-${MSYSTEM}"

  cd "${srcdir}/build-${MSYSTEM}"
  ../${_realname}-${_pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR=${pkgdir} install

  # Fix mod_rasterlite2 location to be possible: SELECT load_extension('mod_rasterlite2');
  mv "${pkgdir}${MINGW_PREFIX}/lib/mod_rasterlite2.dll" "${pkgdir}${MINGW_PREFIX}/bin/"

  install -Dm644 "${srcdir}/${_realname}-${_pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
